Skip to main content

Manipulating Arrays

Some of the methods used for array manipulation are:

  • concat() method
  • join() method
  • split() method
  • reverse() method
  • slice() method
  • splice() method
  • toString() method
  • sort() method

concat() Method

The concat() method is used to concatenate multiple arrays.

Syntax
concat();
concat(value0);
concat(value0, value1);
concat(value0, value1, ... , valueN);
var arr = [1, 2, 3, 4];
console.log(arr.concat("White", "Blue"));
console.log(arr);
Note

By using concat() method the original array is not modified, but instead returns a new array.

join() Method

The join() method is used to convert an array to a string. We can set the separator and this separator is attached with array elements to form a string.

Syntax
join();
join(separator);
var arr = ["red", "blue", "green", "yellow"];
console.log(arr.join(""));
console.log(arr.join("/"));
console.log(arr);
Note

By using join() method the original array is not modified. This method is used to convert an array to a string.

split() Method

The split() method the divides a string into a substrings by searching for the separator that we have given.

Syntax
split();
split(separator);
split(separator, limit);
var arr = "Welcome to Coding Habits";
console.log(arr.split(" "));
console.log(arr.split(" ", 3));
console.log(arr);

Given string is converted to a sub string based on the separator given, in order to limit the split characters or words limit is used. From the example it is clear.

reverse() Method

The reverse() method is used to reverse the elements of an array

Syntax
reverse();
var arr = [1, 2, 3, 4, 5, 6];
console.log("Before reverse");
console.log(arr.reverse());
console.log("After reverse");
console.log(arr);
Note

By using reverse() method, the original array is modified.

slice() Method

The slice() method is used to create a sub array of the original array.

Syntax
slice();
slice(start);
slice(start, end);

The slice() method takes two arguments, start and index and returns an array containing the elements of start and end. The start and end parameters represent the index of the array. If only one argument is given, all the elements starting from that index will be returned. If negative value is given, the elements are taken from the end of the array. For example, calling slice(-5) returns an array of last five elements.

var arr = [1, 2, "White", 4, "Black", 6];
// Only one argument is given
console.log(arr.slice(2));
// If start and end is given
console.log(arr.slice(2, 5));
// If a negative argument is given
console.log(arr.slice(-4));
// If negative start and end is given
console.log(arr.slice(-5, -2));
// console.log("After reverse");
console.log(arr);
Note

By using slice() method the original array is not modified.

splice() Method

The splice() method is used to add, replace or remove elements from an array.

Syntax
splice(start);
splice(start, deleteCount);
splice(start, deleteCount, replaceValues);
splice(start, deleteCount, replaceValues1, replaceValues2, replaceValuesN);

The first argument start is the index at which to perform the operation. The second argument is deleteCount, the number of elements to delete beginning with index start. Any further arguments represented by replacevalues (that are comma-separated, if more than one) are inserted in place of the deleted elements.

The splice methods take three arguments, start is index at which operation starts. The second argument is deleteCount, which is the number of elements to be deleted from the start index. The third argument is replaceValues, which replaces deleteCount elements with replaceValues elements. To insert more replaceValues elements, elements are separated by comma.

var arr = ["Jan", "April", "White", 4, "Black", 6];
arr.splice(1, 2, "one", "two");
console.log(arr);
Note

By using splice() method the original array is modified.

toString() Method

The toString() method is similiar to join() method. The toString() method returns a string which contains comma separated values of the array.

Syntax
toString();
var arr = ["one", "two", "three", "four", "five"];
console.log("Before toString method");
console.log(arr.toString());
console.log("After toString method");
console.log(arr);

sort() Method

The sort() method sorts the array elements according to lexicographic order. It is done by first the array is converted to a string and the sorted based on lexicographically. Due to this, there may be a chance for unexpected results.

Syntax
sort();
var arr = ["one", "two", "three", "seven", "ten"];
var arr1 = [1, 15, 7, 10, 78, 99, 45, 62, 44];
console.log("Before sort method");
console.log(arr.sort());
console.log(arr1.sort());
console.log("After sort method");
console.log(arr.sort());
console.log(arr1.sort());
console.log(arr1);

In array arr after sorting, the elements are sorted correctly, but in array arr1, the elements are not sorted correctly. That is because sorting is based on lexicographic, so from the string ordering perspective 62 > 7. To avoid or solve this problem a custom sort function is to be implemented.